package typeSwitch

import "reflect"

func BoolToInt(param bool) int {
	if param {
		return 1
	} else {
		return 0
	}
}

func Struct2Map(obj interface{}) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)
	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		StructFiel := t.Field(i)
		print(StructFiel.Tag.Get("urlParam"))
		data[t.Field(i).Name] = v.Field(i).Interface()
	}
	return data
}

func Struct2MapUrlParam(obj interface{}) map[string]interface{} {
	return Struct2MapCustomerTag(obj, "urlParam")
}

func Struct2MapCustomerTag(obj interface{}, tag string) map[string]interface{} {
	t := reflect.TypeOf(obj)
	v := reflect.ValueOf(obj)
	var data = make(map[string]interface{})
	for i := 0; i < t.NumField(); i++ {
		StructFiel := t.Field(i)
		if tag != "" {
			name := StructFiel.Tag.Get(tag)
			data[name] = v.Field(i).Interface()
		} else {
			data[t.Field(i).Name] = v.Field(i).Interface()
		}
	}
	return data
}
